package com.xiequn;

import com.utils.ZipUtil;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by full on 2018/4/25.
 */
@Mojo(name = "app-package",defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
public class MainAppPackage extends AbstractMojo {

    @Parameter(defaultValue = "${basedir}", readonly = true)
    private String baseDir;

    @Parameter(defaultValue = "${project.build.directory}",readonly = true)
    private String buildDir;

//    @Parameter(defaultValue = "${dBean}")
//    private DBean bean;

    @Parameter(defaultValue = "${db}")
    private boolean db;

    @Parameter(defaultValue = "${url}")
    private String url;
    @Parameter(defaultValue = "${user}")
    private String user;
    @Parameter(defaultValue = "${password}")
    private String password;
    @Parameter(defaultValue = "${web}")
    private String web;

    @Parameter(defaultValue = "${zips}")
    private String zips;

    public void execute() throws MojoExecutionException, MojoFailureException {

        System.out.println(url+user+password);
        System.out.println(db);
//        getLog().info(bean.toString());

        String resPath = baseDir+web;
        String desPath = baseDir+zips;
        File res = new File(resPath);
        File dest = new File(desPath);

        getLog().info("res:"+res.getAbsolutePath());
        getLog().info("dest:"+dest.getAbsolutePath());
        if (res.exists() && res.isDirectory()) {
            if (dest.exists()) {
                try {
                    FileUtils.forceDelete(dest);
                } catch (IOException e) {
                    getLog().error("文件删除失败",e);
                }
                getLog().info("del:"+dest.getAbsolutePath());
            }

            if (dest.mkdirs()) {
                getLog().info("create:" + dest.getAbsolutePath());
                File[] files = res.listFiles();
                for (File file : files) {
                    if (file.isDirectory()) {
                        String zipName = dest.getAbsolutePath() + File.separator + file.getName() + ".zip";
                        getLog().info("createFile:" + zipName);
                        ZipUtil.zip(file.getAbsolutePath(),zipName);
                    }
                }
            } else {
                getLog().error("文件创建失败:" + dest.getAbsolutePath());
                throw new MojoFailureException("");
            }
        }


        // 更新Mac编码
        DBean bean = new DBean();
        bean.setUrl(url);
        bean.setUser(user);
        bean.setPassword(password);

        try {
            RegisterMacToDB.toMySQL(desPath,bean,db);
            getLog().info("mac编码更新成功");
        } catch (Exception e) {
            getLog().info("mac编码更新失败");
            e.printStackTrace();
        }

    }
}
